//
// Created by liufenglin on 20-4-18.
//
#include <stdlib.h>
#include <string.h>
#include "str.h"

char *strtrim(char *str) {
    if (str == NULL) {
        return str;
    }
    int len = strlen(str);
    int i = 0, m = 0, n = strlen(str);
    for (i = 0; i < len; i++) {
        if (str[i] != ' ' && str[i] != '\t') {
            m = i;
            break;
        }
    }


    for (i = len - 1; i >= 0; i--) {
        if (str[i] != ' ' && str[i] != '\t') {
            n = i + 1;
            break;
        }
    }

    return substr(str, m, n);

}

char *substr(char *str, int from, int to) {
    int i = 0;
    for (i = 0; i < (to - from); i++) {
        str[i] = str[i + from];
    }
    memset(str + to - from, 0, to - from);
    return str;
}

